package com.ndphu.comic.gui.fragment.book;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ndphu.comic.R;
import com.ndphu.comic.gui.fragment.book.OnlineBookChapterListFragment.OnChapterSelectedListener;
import com.ndphu.comic.model.Book;

public class OnlineBookDetailsFragment extends Fragment {

	private Book mBook;
	private ViewPager mPager;
	private SlidingMenuPagerAdapter mPagerAdapter;
	private OnChapterSelectedListener mListener;
	private OnlineBookDescriptionFragment mBookDescriptionFragment;
	private OnlineBookChapterListFragment mChapterListFragment;
	private TextView mBookTitle;
	private int mSelectedChapter = 0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mBookDescriptionFragment = new OnlineBookDescriptionFragment();
		mBookDescriptionFragment.setBookCover(mBook.getCover());
		mBookDescriptionFragment.setBookDescription(mBook.getBookDesc());

		mChapterListFragment = new OnlineBookChapterListFragment();
		mChapterListFragment.setChapterList(getBook().getChapters());
		mChapterListFragment.setOnChapterSelectedListener(mListener);
		mChapterListFragment.setSelectedChapter(mSelectedChapter);

		View view = inflater.inflate(R.layout.fragment_online_book_details, container, false);
		mPager = (ViewPager) view.findViewById(R.id.fragment_online_book_details_content_pager);
		mPagerAdapter = new SlidingMenuPagerAdapter(getFragmentManager());
		mPager.setAdapter(mPagerAdapter);
		mBookTitle = (TextView) view.findViewById(R.id.fragment_online_book_details_title);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mBookTitle.setText(mBook.getName());

	}

	public class SlidingMenuPagerAdapter extends FragmentStatePagerAdapter {

		public SlidingMenuPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0: {
				return mBookDescriptionFragment;
			}
			case 1: {
				return mChapterListFragment;
			}
			default:
				return null;
			}
		}

		@Override
		public float getPageWidth(int position) {
			switch (position) {
			case 0:
				return 0.6f;
			case 1:
				return 0.4f;
			default:
				return 1;
			}
		}

		@Override
		public int getCount() {
			return 2;
		}
	}

	public Book getBook() {
		return mBook;
	}

	public void setBook(Book mBook) {
		this.mBook = mBook;
	}

	public OnChapterSelectedListener getOnChapterSelectedListener() {
		return mListener;
	}

	public void setOnChapterSelectedListener(OnChapterSelectedListener onChapterSelectedListener) {
		this.mListener = onChapterSelectedListener;
	}

	public void changeSelectedChapter(int idx) {
		mSelectedChapter = idx;
		if (mChapterListFragment != null) {
			mChapterListFragment.setSelectedChapter(idx);
		}
	}
}
